#include "MainState.h"
#include "InputManager.h"
#include "EffectManager.h"
#include "Game.h"
#include "Terrain.h"
#include "CityPlan.h"
#include "Roads.h"
#include "River.h"
#include <tchar.h>

MainState MainState::m_MainState;

void MainState::Init()
{
	D3DXFONT_DESC fontDesc;
	fontDesc.Height			 = 18;
	fontDesc.Width			 = 0;
	fontDesc.Weight			 = 0;
	fontDesc.MipLevels		 = 1;
	fontDesc.Italic			 = false;
	fontDesc.CharSet		 = DEFAULT_CHARSET;
	fontDesc.OutputPrecision = OUT_DEFAULT_PRECIS;
	fontDesc.Quality         = DEFAULT_QUALITY;
	fontDesc.PitchAndFamily  = DEFAULT_PITCH | FF_DONTCARE;
	_tcscpy(fontDesc.FaceName, (L"Times New Roman"));

	D3DXCreateFontIndirect(TheD3DApp::Instance()->GetD3DDev(), &fontDesc, &m_Font);

	m_EffectName = "AmbientShader";
	m_pEffect = new Effect();
	m_pEffect->Load(L"AmbientShader.fx");

	TheEffectManager::Instance()->AddEffect(m_EffectName, m_pEffect);
	TheEffectManager::Instance()->Get(m_EffectName)->SetTech("RenderScene");

	//Create Objects
	Terrain* t = new Terrain;
	t->Create(32,10,D3DXVECTOR3(0,0,0));
	t->SetTexture(L"UV.jpg");
	m_vGameObjects.push_back(t);
	SwitchGO[0]=t;
	SwitchGO[1]=&t->GetFractal()->GetGraph();
	m_vGameObjects.push_back(SwitchGO[1]);

	//River* riv = new River;
	//riv->Generate(t);
	//m_vGameObjects.push_back(riv);
	//SwitchGO[2]=riv;
	
	CityPlan* c = new CityPlan;
	m_vGameObjects.push_back(c);
	c->CreateVTess(t);
	SwitchGO[2]=c;
	/*
	Roads* r= new Roads;
	r->Generate(c->GetVTess());
	m_vGameObjects.push_back(r);
	*/
	for(unsigned int i=3;i<11;++i)
	{
		SwitchGO[i]=m_vGameObjects[m_vGameObjects.size()-(i-1)];
	}
	
	m_pCamera = new Camera;
	m_pCamera->Load("Static");

	//Default Light
	m_vLightDir.x = 10.0;
	m_vLightDir.y = -10.0;
	m_vLightDir.z = 0.0;

	// Fill in a light structure defining our light
	D3DLIGHT9 light;
	ZeroMemory( &light, sizeof(D3DLIGHT9) );
	light.Type       = D3DLIGHT_DIRECTIONAL;
	light.Diffuse.r  = 0.4f;
	light.Diffuse.g  = 0.4f;
	light.Diffuse.b  = 0.4f;
	light.Diffuse.a  = 1.0f;
	light.Range      = 10.0f;
	D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &m_vLightDir );

	// Tell the device about the light and turn it on
	TheD3DApp::Instance()->GetD3DDev()->SetLight( 0, &light );
	TheD3DApp::Instance()->GetD3DDev()->LightEnable( 0, TRUE ); 
	TheD3DApp::Instance()->GetD3DDev()->SetRenderState( D3DRS_LIGHTING, TRUE );
	TheD3DApp::Instance()->GetD3DDev()->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_XRGB(50,50,50) );
}
void MainState::Clean()
{
	for(unsigned int i=0;i<m_vGameObjects.size();++i)
	{
		m_vGameObjects[i]->Clean();
	}
	m_vGameObjects.clear();
}

void MainState::Pause()
{

}
void MainState::Resume()
{

}

void MainState::HandleInput()
{
	int scanMax=min(m_vGameObjects.size(),9);
	for (unsigned int i=0;i<scanMax;++i)
	{
		if(TheInputManager::Instance()->KeyPressed(i+2) && !switches[i])
		{
			SwitchGO[i]->SetDraw(!SwitchGO[i]->GetDraw());
			switches[i]=true;
		}
		else if(!TheInputManager::Instance()->KeyPressed(i+2))
		{
			switches[i]=false;
		}
	}

	if(TheInputManager::Instance()->KeyPressed(DIK_P) && !pressed)
	{
		TheEffectManager::Instance()->SetWireframe(!TheEffectManager::Instance()->GetWireframe());
		pressed=true;
	}
	else if (!TheInputManager::Instance()->KeyPressed(DIK_P))
	{
		pressed=false;
	}

	if(TheInputManager::Instance()->KeyPressed(DIK_L) && !switches[11])
	{
		TheEffectManager::Instance()->SetBasic(!TheEffectManager::Instance()->GetBasic());
		switches[11]=true;
	}
	else if (!TheInputManager::Instance()->KeyPressed(DIK_L))
	{
		switches[11]=false;
	}
	if(TheInputManager::Instance()->KeyPressed(DIK_LEFT))
	{
		m_vGameObjects[0]->SetPos(m_vGameObjects[0]->GetPos()+D3DXVECTOR3(0.1,0,0));
	}
	else if(TheInputManager::Instance()->KeyPressed(DIK_RIGHT))
	{
		m_vGameObjects[0]->SetPos(m_vGameObjects[0]->GetPos()-D3DXVECTOR3(0.1,0,0));
	}
	if(TheInputManager::Instance()->KeyPressed(DIK_UP))
	{
		m_vGameObjects[0]->SetPos(m_vGameObjects[0]->GetPos()+D3DXVECTOR3(0,0,0.1));
	}
	else if(TheInputManager::Instance()->KeyPressed(DIK_DOWN))
	{
		m_vGameObjects[0]->SetPos(m_vGameObjects[0]->GetPos()-D3DXVECTOR3(0,0,0.1));
	}
}
void MainState::HandleCollisions()
{

}
void MainState::Update(float dt)
{

	for(unsigned int i=0;i<m_vGameObjects.size();++i)
	{
		m_vGameObjects[i]->Update(dt);
	}
	TheInputManager::Instance()->Update();
	m_pCamera->Update();
}

void MainState::Render()
{	
	
	Effect* effect = TheEffectManager::Instance()->Get(m_EffectName);

	D3DXMATRIX world;
	D3DXMatrixIdentity(&world);

	world *= (m_pCamera->GetViewMatrix() * m_pCamera->GetProjectionMatrix());

	TheEffectManager::Instance()->Render(m_EffectName, m_pCamera, m_vGameObjects);

	RECT r = {6,6,1,1};
	static char buffer[16];
	sprintf(buffer,"FPS: %d",TheGame::Instance()->GetFPS());
	m_Font->DrawTextA(0,buffer, -1, &r, DT_CALCRECT, D3DCOLOR_XRGB(250,250,250));
	m_Font->DrawTextA(0,buffer, -1, &r, 0, D3DCOLOR_XRGB(250,250,250));
	
	TheD3DApp::Instance()->GetD3DDev()->EndScene();

	TheD3DApp::Instance()->GetD3DDev()->Present(NULL, NULL, NULL, NULL);

}